<template>
  <div>
    <!-- 按钮 -->

    <el-button style="margin:10px 10px" type="primary" icon="el-icon-refresh-right" circle
      @click="getDynamicExtensionProcessList"></el-button>
    <el-button type="primary" @click="add()">添加</el-button>
    <el-dropdown @command="handleCommand" style="margin-left: 50px;">
      <span class="el-dropdown-link">
        {{sName || '广告类型'}}<i class="el-icon-arrow-down el-icon--right"></i>
      </span>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item :command="index" v-for="(v,index) in sortList" :key="v.name">{{v.name}}</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>

    <el-table style="width: 100%;" border :data="list">
      <el-table-column prop="name" label="套餐名" align="center">
      </el-table-column>
      <el-table-column prop="intro" label="介绍" align="center">
      </el-table-column>
      <!--      <el-table-column prop="estimatedTime" label="预计时间(分钟)" align="center">
      </el-table-column> -->
      <el-table-column prop="costMoney" label="金额花费" align="center">
      </el-table-column>
      <el-table-column prop="estimatedTime" label="投放天数" align="center">
      </el-table-column>
<!--      <el-table-column prop="exposureNum" label="预计曝光量" align="center">
      </el-table-column> -->
      <el-table-column prop="type" label="广告类型" align="center">
        <template slot-scope="{ row, $index }">
          <!--  <div>{{ row.type == "1" ? '用户广告' : '商业广告' }}</div> -->
          <div v-if="row.type == 1">用户广告</div>
          <div v-if="row.type == 2">开屏广告</div>
          <div v-if="row.type == 3">视频广告</div>
        </template>
      </el-table-column>
      <el-table-column prop="isCustom" label="是否自定义" align="center">
        <template slot-scope="{ row, $index }">
          <div>{{ row.isCustom == "0" ? '否' : '是' }}</div>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="{ row, $index }">
          <el-tooltip content="修改套餐" placement="top" slot="reference">
            <el-button type="primary" icon="el-icon-edit" @click="ubpateBannar(row)"
              :disabled="!list.isCustom ? false : true"></el-button>
          </el-tooltip>
          <el-popconfirm title="是否删除套餐？" icon="el-icon-info" icon-color="red" @confirm="getDelete(row.id)">
            <el-tooltip content="删除套餐" placement="top" slot="reference">
              <el-button type="danger" icon="el-icon-delete"></el-button>
            </el-tooltip>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 对话框  -->
    <el-dialog title="修改套餐" :visible.sync="dialogFormVisible">
      <el-form style="width:80%" :model="tmFrom" ref="ruleForm">
        <el-form-item label="套餐名" label-width="100px" prop="name">
          <el-input autocomplete="off" v-model="tmFrom.name"></el-input>
        </el-form-item>
        <el-form-item label="介绍" label-width="100px" prop="intro">
          <el-input autocomplete="off" v-model="tmFrom.intro"></el-input>
        </el-form-item>
        <el-form-item label="金额花费" label-width="100px" prop="costMoney">
          <el-input autocomplete="off" v-model="tmFrom.costMoney"></el-input>
        </el-form-item>
        <el-form-item label="投放天数" label-width="100px" prop="estimatedTime">
          <el-input autocomplete="off" v-model="tmFrom.estimatedTime"></el-input>
        </el-form-item>
<!--        <el-form-item label="预计曝光量" label-width="100px" prop="exposureNum">
          <el-input autocomplete="off" v-model="tmFrom.exposureNum"></el-input>
        </el-form-item> -->
        <el-form-item label="广告类型" label-width="100px">
          <el-select v-model="tmFrom.type" placeholder="请选择广告类型" prop="type">
            <el-option label="用户广告" :value="1"></el-option>
            <el-option label="开屏广告" :value="2"></el-option>
            <el-option label="视频广告" :value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否自定义" label-width="100px">
          <el-select v-model="tmFrom.isCustom" placeholder="请选择是否自定义" prop="isCustom">
            <el-option label="是" :value="1"></el-option>
            <el-option label="否" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <!--      <el-form-item label="预计时间/分钟" label-width="100px" prop="estimatedTime">
          <el-input autocomplete="off" v-model="tmFrom.estimatedTime"></el-input>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="UpdateBillingRules">修改</el-button>
      </div>
    </el-dialog>

    <!-- 添加对话框  -->
    <el-dialog title="添加套餐" :visible.sync="dialogFormVisible1">
      <el-form style="width:80%" :model="list1" :rules="rules1" ref="ruleForm">
        <el-form-item label="套餐名" label-width="100px" prop="name">
          <el-input autocomplete="off" v-model="list1.name"></el-input>
        </el-form-item>
        <el-form-item label="介绍" label-width="100px" prop="intro">
          <el-input autocomplete="off" v-model="list1.intro"></el-input>
        </el-form-item>
        <el-form-item label="金额花费" label-width="100px" prop="costMoney">
          <el-input autocomplete="off" v-model="list1.costMoney"></el-input>
        </el-form-item>
        <el-form-item label="投放天数" label-width="100px" prop="estimatedTime">
          <el-input autocomplete="off" v-model="list1.estimatedTime"></el-input>
        </el-form-item>
      <!--  <el-form-item label="预计曝光量" label-width="100px" prop="exposureNum">
          <el-input autocomplete="off" v-model="list1.exposureNum"></el-input>
        </el-form-item> -->
        <!--        <el-form-item label="预计时间/分钟" label-width="100px" prop="estimatedTime">
          <el-input autocomplete="off" v-model="list1.estimatedTime"></el-input>
        </el-form-item> -->
        <el-form-item label="广告类型" label-width="100px">
          <el-select v-model="list1.type" placeholder="请选择广告类型" prop="type">
            <el-option label="用户广告" value="1"></el-option>
            <el-option label="开屏广告" value="2"></el-option>
            <el-option label="视频广告" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否自定义" label-width="100px">
          <el-select v-model="list1.isCustom" placeholder="请选择是否自定义" prop="isCustom">
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="getAddDynamicExtensionConfiguration">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'Dynamic',
    data() {

      return {
        sName: '用户广告',
        typeId: 1,
        sortList: [{
            name: '用户广告',
            type: 1
          },
          {
            name: '开屏广告',
            type: 2
          },
          {
            name: '视频广告',
            type: 3
          }
        ],
        // 修改时的数据
        //列表数据
        list: [],
        // 添加列表
        list1: {
          // // 金币花费
          // costGold: "",
          // // 预计时间
          // estimatedTime: "",
          // // 名称
          // intro: "",
          // // 是否自定义
          // isCustom: "",
          // // 预计播放量
          // playNum: ""
        },

        // 对话框显示与隐藏控制
        dialogFormVisible: false,
        dialogFormVisible1: false,
        // 表单对象
        tmFrom: {},
        // 当前为修改还是添加 修改为false 添加为true
        state: true,
        // 表单验证

        rules1: {
          // intro: [
          //   { required: true, message: '请输入活动名称', trigger: 'chang' },
          //   { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          // ],
          // costGold: [
          //   { required: true, message: '请输入金币花费', trigger: 'int' },
          //   // { min: 3, max: 5, message: '请输入金币花费', trigger: 'blur' }
          // ],
          // playNum: [
          //   { required: true, message: '请输入预计播放量', trigger: 'int' },
          //   // { min: 1, max: 99999, message: '请输入预计播放量', trigger: 'int' }
          // ],
          // estimatedTime: [
          //   { required: true, message: '请输入预计时间', trigger: 'change' },
          //   // { min: 1, max: 9999, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          // ]
        },
      }
    },
    mounted() {
      this.getAdvertisementConfigurationList()
    },

    methods: {
      handleCommand(command) {
        // console.log(command)
        this.sName = this.sortList[command].name
        // this.getQuestionAnswerList(this.sortList[command].id)
        this.typeId = this.sortList[command].type
        this.getAdvertisementConfigurationList()
      },
      // 获取套餐列表
      async getAdvertisementConfigurationList() {
        let res = await this.$API.system.getAdvertisementConfigurationList({
          type: this.typeId,
        })
        console.log(res, "108")
        this.list = res.data.configList;
        if (res.data.customConfig.id != null) {
          this.list.push(res.data.customConfig)
        }



      },
      // 刷新
      getDynamicExtensionProcessList() {
        this.getAdvertisementConfigurationList(this.typeId)
      },
      // 添加一条数据窗口弹出
      add() {
        // this.state = true
        this.dialogFormVisible1 = true
        this.list1 = {
          // // 金币花费
          // costGold: "",
          // // 预计时间
          // estimatedTime: "",
          // // 名称
          // intro: "",
          // // 是否自定义
          // isCustom: "",
          // // 预计播放量
          // playNum: ""
        }
      },
      // 添加一条数据发送到后端
      getAddDynamicExtensionConfiguration() {
        this.$refs.ruleForm.validate(async (valid) => {
          if (valid) {
            let res = await this.$API.system.addAdvertisementConfiguration(this.list1);
            console.log(res);
            this.dialogFormVisible1 = false;
            this.getAdvertisementConfigurationList()
          } else {
            console.log('error submit!!');
            return false;
          }
        })

        // console.log(this.tmFrom);
      },
      // 点击修改
      ubpateBannar(row) {
        this.state = false
        this.dialogFormVisible = true;
        // 浅拷贝  让tmFrom收到的数据非服务器直接传回的数据
        this.tmFrom = {
          ...row
        };
        console.log(this.tmFrom);
      },
      // 点击修改
      UpdateBillingRules() {
        // console.log(this.id);
        this.$refs.ruleForm.validate(async (valid) => {
          if (valid) {
            let res = await this.$API.system.updateAdvertisementConfiguration(this.tmFrom);
            this.dialogFormVisible = false;
            this.getAdvertisementConfigurationList()
          } else {
            console.log('error submit!!');
            return false;
          }
        })
      },
      // 删除
      async getDelete(id) {
        let res = await this.$API.system.deleteAdvertisementConfiguration({
          id: id
        });
        console.log(res);
        this.getAdvertisementConfigurationList()
      }
    }
  }
</script>

<style lang="scss">
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
